package com.ruoyi.api;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.ruoyi.program.domain.AliToken;

@RestController
@RequestMapping("/test/api")
public class TestApi {


  @GetMapping("/aaaa")
  public String test(

      @RequestParam(value = "app_auth_code") String appAuthCode,
      @RequestParam(value = "state") String state,
      @RequestParam(value = "app_id") String appId,
      @RequestParam(value = "source") String source) {
       
      System.out.println();
    //查询数据库appid
    String privateKey  = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCFthyJvfDhYYIjKemkucugb6Qhac9UfY6fmgp+VvSDMkyypoMQnH4APNYDSYAaSMClMWPAacEXcLpBPF/jJ/xsGbczprJTe2ZYZJr/nQChsQI4i4gpqm//3MR1oGjpOIU0zr2BNKaIPF5AhOD1M/Y+/SJ7ZCfO1BYhot/zLxZDtNxD3MvKscDH0Iie/opSNN4sAVJ/VA099ax3yfryV3VpjU3g+p5X8hUzjotrrCkSlot53dI9LrF1E3utlisa+aJH5FdJGn71kwVmpFuy6VbZt27faMO3kTpuR+l1yAZFtyHlhmn8lDopOPRzakv7SX0EpyEpUHLRGPiAr3JB9fDjAgMBAAECggEAR/tW96wiTiLhHS8GEeDXx0HyuO4uaEzDiBY0Sflj3nEoqxBLBJlKl7QoflKGPSAnXYbuLXqBMa5FBrOK503ybPuXDZ5S5/BJk5Jtgugxm9of2PL81Xlkm/hsZup5L/x2cYxOpo0Cjdr53n/qXvafPR+m/YhYidbRJ0B4kYEq2j1u26Bo+fgq7oQRSmnuQuxolXvJEd8s4k4jr4A5CWz8OllN/wedHjzydtopQ6iuAy9gcLcbW6qNpfKA0yzp93JSZ6ZveAk9up55C9yhSmHUrSKYsM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s1TuNSuMVEJPdk79yAPYukIipWB/kkRqjQviarDvVKVbXdDYx8L/orriNwIVDVYmu/YYxwDhfy0ohYEKN7kLGtfHtl3zg56jrDJ4PcXuKcxJRO51MjgHvNL7JTdcRDbtMfZyUc3ViSJHnwUlHd3OFNJOWAC44h+HTWedF2/MUQUrM/DAC1M1mdoROlZj1RDWIZdrxKdceM1bmuwkhzOCziT/DSEzAZgX25z+ZqCAG19DGZ9eWtmmCl5RIJyoOxPWJQIDAQAB";
    AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId,
    privateKey, "json", "GBK", alipayPublicKey, "RSA2");
    AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
    request.setGrantType("authorization_code");
    request.setCode(appAuthCode);

    // 为解决 response 无法解析的问题，需要将 response 变量的作用域扩大
    // 原代码中 response 定义在 try 块内，在块外无法访问，因此将其定义移到 try 块外
    AlipaySystemOauthTokenResponse response = null;
    try {
      response = alipayClient.execute(request);
    } catch (com.alipay.api.AlipayApiException e) {
      e.printStackTrace();
      // 可以根据实际需求添加更多的异常处理逻辑
    }

    if (response != null && response.isSuccess()) {
      //写库
      AliToken token = new AliToken();
      token.setState(state);


// 假设 AliTokenMapper 有 insert 方法用于插入数据，将 save 替换为 insert
      // aliTokenMapper.insert(token);
      
      System.out.println("调用成功");
    } else {
      System.out.println("调用失败");
    }

    // 打印参数到控制台（实际应用中你可能会想做其他处理）
    System.out.println("App Auth Code: " + appAuthCode);
    System.out.println("State: " + state);
    System.out.println("App ID: " + appId);
    System.out.println("Source: " + source);
    return "<h1>Request Received</h1>";
  }
}
